import 'package:flutter/material.dart';
import 'package:fusion_one/redux/store/index.dart';
import '../view/login/login.dart';
import '../view/home.dart';

class RouterTable {
  static String loginPath = 'login';
  static String homePath = '/';

  static Map<String, WidgetBuilder> routeTables = {
    //登录
    loginPath: (context) => const LoginPage(),
    //首页
    homePath: (context) => const MyHomePage(),
  };

  ///[settings] 路由配置
  static Route? onGenerateRoute(RouteSettings settings) {
    // 登录拦截
    if (settings.name == loginPath) {
      return MaterialPageRoute(
        builder: routeTables[settings.name]!,
      );
    }
    if (store.state.token?.access_token == null) {
      return MaterialPageRoute(
        builder: (context) => const LoginPage(),
      );
    }
    return MaterialPageRoute(
      builder: routeTables[settings.name]!,
    );
  }
}
